<?php
spAddViewFunction('str_replace', 'strreplaceone');
spAddViewFunction('str_replacearr', 'strreplacearr');
spAddViewFunction('save_off', 'saveoff');
spAddViewFunction('Adminname', 'getname');
spAddViewFunction('get_day', 'getday');
spAddViewFunction('FormatDateAgo', 'format_date_ago');
spAddViewFunction('GetNewsCategories', 'get_newscategories');
spAddViewFunction('GetNewsTag', 'get_news_tag');
spAddViewFunction('GetCommentNewsCount', 'get_comment_news_count');
spAddViewFunction('GetCommentNewsList', 'get_comment_news_list');
spAddViewFunction('GetPage', 'Get_page');
spAddViewFunction('GetBreadcrumbNav', 'Get_BreadCrumb');
spAddViewFunction('GetProductBreadcrumbNav', 'Get_Product_BreadCrumb');
spAddViewFunction('GetPrevious', 'Get_Previous');
spAddViewFunction('GetNext', 'Get_Next');
spAddViewFunction('GetNewsList', 'Get_News_List');
spAddViewFunction('GetNewsList2', 'Get_News_List2');
spAddViewFunction('GetNewsListDiv', 'Get_News_List_Div');
spAddViewFunction('GetAboutList', 'Get_About_list');
spAddViewFunction('Glhtml', 'Gl_html');
spAddViewFunction('Productcount', 'Product_count');
spAddViewFunction('Newscount', 'News_count');
spAddViewFunction('RelatedProduct', 'Related_Product');
spAddViewFunction('Newproducts', 'New_products');
spAddViewFunction('NewproductsDiv', 'New_products_div');
spAddViewFunction('Getprice', 'Get_price');
spAddViewFunction('Returnprice', 'return_price');
spAddViewFunction('Productslist', 'Products_list');
spAddViewFunction('Productsize', 'Product_size');
spAddViewFunction('Getproducturl', 'Get_product_url');
spAddViewFunction('GetPreviousproduct', 'Get_Previous_product');
spAddViewFunction('GetNextproduct', 'Get_Next_product');
spAddViewFunction('FootOther', 'footother');
spAddViewFunction('GetCommentCount', 'get_comment_count');
spAddViewFunction('GetCommentList', 'get_comment_list');
spAddViewFunction('GetDiscount', 'Get_Discount');
spAddViewFunction('Getpriceabc', 'Get_price_abc');
spAddViewFunction('GetLinklist', 'get_Link_list');
spAddViewFunction('Productslist2', 'Products_list2');
/**
 * 返回最新信息评论
 * @param $num 条数
 */
function get_Link_list($params)
{
    if (!is_array($params))
        return '';
    $type = $params['type'];
    $num = $params['num'];
    $jg = $params['jg'];
    $request = spClass('linkModel')->get_link_type($type);
    foreach ($request as $k => $v) {
        $list .= "\n" . $jg[0] . "\n";
        $list .= '<a href="' . $v['link_url'] . '" target="_blank">' . $v['link_title'] .
            '</a>';
        $list .= "\n" . $jg[1] . "\n";
        $i = $i + 1;
    }
    return $list;
}
/**
 * 单纯的返回价1或价2 价2存在返回价2
 *@param $price 价1
 *@param $price2 价2
 */
function Get_price_abc($params)
{
    $price = $params['price'];
    $price2 = $params['price2'];
    if ($price2 <> '')
        return $price2;
    return $price;
}
/**
 * 返回新闻信息数量
 */
function News_count($params)
{
    $categories_id = $params['cid'];
    if ($categories_id) {
        $son = spClass("newscategoriesModel")->spCache(3600 * 24)->Categories_son($categories_id);
        foreach ($son as $k => $v) {
            if ($k == 0) {
                $cidarr = $v['categories_id'];
            } else {
                $cidarr .= ',' . $v['categories_id'];
            }
        }
        $categories_id = ($cidarr <> '') ? $cidarr : $categories_id;
    }
    return spClass('newsModel')->News_count($categories_id);
}

/**
 * 返回优惠活动价格
 */
function Get_Discount($params)
{
    $Discount = $params['Discount'];
    $item = $params['item'];
    $Currency = spClass('currenciesModel')->Get_currency($_SESSION['currencies_id']);
    $symbol_left = $Currency['symbol_left']; //当前汇率符号 ￥$
    $value = $Currency['value']; //当前汇率0.63
    return $Discount * $item * $value;
}
/**
 * 根据信息ID返回评论数
 */
function get_comment_count($params)
{
    $products_id = $params['productsid'];
    $condition = array('products_id' => $products_id);
    $count = spClass('commentsModel')->spCache(3600 * 24)->findCount($condition);
    return $count;
}
/**
 * 返回最新信息评论
 * @param $num 条数
 */
function get_comment_list($params)
{
    if (!is_array($params))
        return '';
    $c = $params['c'];
    $a = $params['a'];
    $num = $params['num'];
    $jg = $params['jg'];
    $request = spClass('commentsModel')->comment_list($num);
    foreach ($request as $k => $v) {
        $news = spClass('productsModel')->FindformID($v['products_id']);

        $list .= "\n" . $jg[0] . "\n";
        $list .= '<span class="side-comment"><a href="' . spUrl($c, $a, array('alias' =>
                $news['alias'])) . '#comments' . $v['comments_id'] . '"> Guest' . $v['comments_id'] .
            '</a>';
        $list .= ' Say: <a href="' . spUrl($c, $a, array('alias' => $news['alias'])) .
            '#comments' . $v['comments_id'] . '">' . htmlspecialchars($v['review_text']) .
            '</a></span>' . "\n";
        $list .= "\n" . $jg[1] . "\n";
        $i = $i + 1;
    }
    return $list;
}

/**
 * 返回上一条信息
 * @param $c 控制器
 * @param $a 动作
 * @param $cid 父类ID
 * @param $type 1为图片显示 2为文字显示
 */
function Get_Previous_product($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $cid = $params['cid'];
    $type = $params['type'];
    $productsid = $params['pid'];
    $up = spClass('productsModel')->Previous($cid, $productsid);
    $str = ($type == 1) ? '<img src="' . BASEPATH .
        '/css/img/button_prev.gif" alt="Previous" title=" Previous "/>' : '' . T('Previous Page') .
        ':' . $up['detail']['products_name'];
    $str = '<a href="' . spUrl($c, $a, array('alias' => $up['alias'])) .
        '" rel="prev">' . $str . '</a>';
    return $str;
}

/**
 * 返回下一条信息
 * @param $c 控制器
 * @param $a 动作
 * @param $cid 父类ID
 * @param $type 1为图片显示 2为文字显示
 */
function Get_Next_product($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $cid = $params['cid'];
    $type = $params['type'];
    $productsid = $params['pid'];
    $up = spClass('productsModel')->Next($cid, $productsid);
    $str = ($type == 1) ? '<img src="' . BASEPATH .
        '/css/img/button_next.gif" alt="Next" title=" Next "/>' : '' . T('Next Page') .
        ':' . $up['detail']['products_name'];
    $str = '<a href="' . spUrl($c, $a, array('alias' => $up['alias'])) .
        '" rel="next">' . $str . '</a>';
    return $str;
}

/**
 * 根据尺码所属ID显示尺码
 */
function Product_size($params)
{
    $size = $params['size'];
    $Attribs = ATTRIBS;
    $Attribs = trim($Attribs);
    $arr = explode(chr(13), $Attribs);
    foreach ($arr as $k => $v) {
        $arr[$k] = explode('|', trim($v));
        foreach ($arr[$k] as $k2 => $v2) {
            $arr2[$k][$k2] = explode(':', trim($v2));
        }
    }
    //$size=$arr2;
    //尺码属性
    //$size = array('0' => array('EUR 35/US 5', 'EUR 36/US 6', 'EUR 37/US 7','EUR 38/US 8', 'EUR 39/US 9', 'EUR 40/US 10', 'EUR 41/US 11'), '1' => array(), '2' => array('M', 'S', 'XS', 'XSS'));
    foreach ($arr2 as $k => $v) {
        if ($v[0][0] == $size) {
            $sizearr = $v[2];
        }
    }
    foreach ($sizearr as $k => $v) {
        $sizestr .= '<option value="' . $v . '">' . $v . '</option>';
    }
    return $sizestr;
}
/**
 * 根据商品ID返回链接地址
 */
function Get_product_url($params)
{
    $pid = (int)$params['pid'];
    if (!$pid)
        return;
    $products = spClass('productsModel')->FindformID($pid);
    return spUrl('products', 'view', array('alias' => $products['alias']));
}

/**
 * 商品价格显示方式
 * pid  商品ID必须要有
 * price 原价
 * price2 优惠价
 * price_type 价格显示方式
 * strtype 1为显示价格 其它带货币符号输出算出汇率
 */
function Get_price($params)
{
    $pid = $params['pid'];
    $price = $params['price'];
    $price2 = $params['price2'];
    $price_type = $params['price_type'];
    $strtype = $params['strtype'];
    $GDcart = $params['GDcart'];
    $yh = $params['yh'];
    if (!$pid)
        return;
    $price_type = ($price_type <> '') ? $price_type : PRICE_TYPE; //商品价格显示机制
    $price_gd = PRICE_GD; //固定价格,只有固定机制才有效
    switch ($price_type) {
        case 1: //不限显示优惠价
            $arr = array(
                'pid' => $pid,
                'price' => $price,
                'price2' => $price2,
                'type' => 1,
                'yh' => $yh,
                'strtype' => $strtype);
            $str = return_price($arr);
            break;
        case 2: //只有会员显示优惠价
            $arr = array(
                'pid' => $pid,
                'price' => $price,
                'price2' => $price2,
                'type' => 2,
                'yh' => $yh,
                'strtype' => $strtype);
            $str = return_price($arr);
            break;
        case 3: //只有会员显示固定价
            $arr = array(
                'pid' => $pid,
                'price' => $price,
                'price2' => $price_gd,
                'type' => 2,
                'yh' => $yh,
                'strtype' => $strtype);
            $str = return_price($arr);
            break;
        case 4: //不限显示固定价
            $arr = array(
                'pid' => $pid,
                'price' => $price,
                'price2' => $price_gd,
                'type' => 1,
                'yh' => $yh,
                'strtype' => $strtype);
            $str = return_price($arr);
            break;
        case 5: //只在购物车中显示固定价
            $arr = array(
                'pid' => $pid,
                'price' => $price,
                'type' => 1,
                'yh' => $yh,
                'price_gd' => $price_gd,
                'GDcart' => $GDcart,
                'strtype' => $strtype);
            $str = return_price($arr);
            break;
        default: //不限会员显示优惠价
            $arr = array(
                'pid' => $pid,
                'price' => $price,
                'price2' => $price2,
                'type' => 1,
                'yh' => $yh,
                'strtype' => $strtype);
            $str = return_price($arr);
            break;
    }


    return $str;
}
/**
 * 返回商品价格
 * pid  商品ID必须要有
 * price
 * price2
 * type 1不限制会员显示优惠价  2只有会员显示优惠价
 * strtype 只返回价格，不带打折字符串
 */
function return_price($params)
{
    $pid = $params['pid'];
    $price = $params['price'];
    $price_discounted = $params['price2'];
    $type = $params['type'];
    $price_gd = $params['price_gd'];
    $GDcart = $params['GDcart'];
    $strtype = $params['strtype'];
    $strtype = ($strtype == 1) ? 1 : 2;
    $yh = $params['yh'];
    if (!$pid)
        return;
    if (!$price) {
        $product = spClass('productsModel')->FindformID($pid);
        $price = $product['products_price'];
        $price_discounted = $product['products_discounted'];
    }
    switch ($type) {
        case 1: //不限会员显示优惠价
            $memberview = true;
            break;
        case 2: //限会员
            $customers = spClass('customersModel')->online();
            $memberview = (isset($customers['customers_id'])) ? true : false;
            break;
    }
    if ($strtype == 2) {
        //获取当前汇率

        $Currency = spClass('currenciesModel')->Get_currency($_SESSION['currencies_id']);
        $symbol_left = $Currency['symbol_left']; //当前汇率符号 ￥$
        $value = $Currency['value']; //当前汇率0.63
        $code = $Currency['code']; //当前汇率符号CNY
        $price = $price * $value;
        if ($price_discounted > 0 and $memberview) {
            $price_discounted = $price_discounted * $value;
            $PriceDiscount = saveoff(array('yj' => $price, 'zh' => $price_discounted));
            $str = '<span class="normalprice"><s>' . $symbol_left . $price .
                ' </s></span> <span class="productSpecialPrice"> ' . $symbol_left . $price_discounted .
                '</span>';
            $str .= ($yh == 1) ? ' <br/><span class="productPriceDiscount">' . T('YHSAVE') .
                ': ' . $PriceDiscount . ' ' . T('YHoff') . '</span>' : '';
        } else {
            $str = '<span class="price">' . $symbol_left . $price . '</span>';
        }
    } else {
        $str = ($price_discounted > 0 and $memberview) ? $price_discounted : $price;
    }
    if ($GDcart == 5) {
        return $price_gd;
    }
    return $str;
}
/**
 * 返回商品数量
 */
function Product_count($params)
{
    $categories_id = $params['cid'];
    if ($categories_id) {
        $son = spClass("categoriesModel")->spCache(3600 * 24)->Categories_son($categories_id);
        foreach ($son as $k => $v) {
            if ($k == 0) {
                $cidarr = $v['categories_id'];
            } else {
                $cidarr .= ',' . $v['categories_id'];
            }

        }
        $categories_id = ($cidarr <> '') ? $cidarr : $categories_id;
    }
    return spClass('productsModel')->Product_count($categories_id);
}
/**
 * 商品列表
 * @param $order 排序 排序 1随机 2最新 3最旧 4价高到底 5价低到高 6销售排行 7人气 
 * @param $type 1全部 2推荐商品
 */
function Products_list($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $order = $params['order'];
    $jg = $params['jg'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $loop = $params['loop'];
    $type = $params['type'];
    $width = $params['w'];
    $height = $params['h'];
    $width = ($width <> '') ? ' width="' . $width . '"' : '';
    $height = ($height <> '') ? ' height="' . $height . '"' : '';
    if ($categories_id) {
        $son = spClass("categoriesModel")->spCache(3600 * 24)->Categories_son($categories_id);
        foreach ($son as $k => $v) {
            if ($k == 0) {
                $cidarr = $v['categories_id'];
            } else {
                $cidarr .= ',' . $v['categories_id'];
            }

        }
        $categories_id = ($cidarr <> '') ? $cidarr : $categories_id;
    }
    $selllist = spClass('productsModel')->New_products_in($categories_id, $num, $order,
        $type);
    foreach ($selllist as $k => $v) {
        $list .= $jg[0];
        $list .= '<a class="thumb" href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['products_title'] . '"><img src="' . WEBDIR .
            'upload/' . $v['products_image'] . '"' . $width . $height . ' alt="' . $v['detail']['products_title'] .
            '" /></a>' . "\n";
        $list .= '<div class="mps_lc_mask"><h5>' . Get_price(array(
            'pid' => $v['products_id'],
            'price' => $v['products_price'],
            'yh' => 1,
            'price2' => $v['products_price_discounted'])) . '</h5>';
        $list .= '<p class="bsw"><a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['products_title'] . '">' . $v['detail']['products_name'] .
            '</a></p></div>' . "\n" . '';
        $list .= $jg[1];
    }
    return $list;
}
/**
 * 商品列表
 * @param $order 排序 排序 1随机 2最新 3最旧 4价高到底 5价低到高 6销售排行 7人气 
 * @param $type 1全部 2推荐商品
 */
function Products_list2($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $order = $params['order'];
    $jg = $params['jg'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $loop = $params['loop'];
    $type = $params['type'];
    $width = $params['w'];
    $height = $params['h'];
    $width = ($width <> '') ? ' width="' . $width . '"' : '';
    $height = ($height <> '') ? ' height="' . $height . '"' : '';
    if ($categories_id) {
        $son = spClass("categoriesModel")->spCache(3600 * 24)->Categories_son($categories_id);
        foreach ($son as $k => $v) {
            if ($k == 0) {
                $cidarr = $v['categories_id'];
            } else {
                $cidarr .= ',' . $v['categories_id'];
            }

        }
        $categories_id = ($cidarr <> '') ? $cidarr : $categories_id;
    }
    $selllist = spClass('productsModel')->New_products_in($categories_id, $num, $order,
        $type);
    foreach ($selllist as $k => $v) {
        $list .= $jg[0];
        $list .= '<a class="thumb" href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['products_title'] . '"><img src="' . WEBDIR .
            'upload/' . $v['products_image'] . '"' . $width . $height . ' alt="' . $v['detail']['products_title'] .
            '" /></a>' . "\n";
        $list .= '<h5>' . Get_price(array(
            'pid' => $v['products_id'],
            'price' => $v['products_price'],
            'yh' => 1,
            'price2' => $v['products_price_discounted'])) . '</h5>';
        $list .= '<p class="bsw"><a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['products_title'] . '">' . $v['detail']['products_name'] .
            '</a></p>' . "\n" . '';
        $list .= $jg[1];
    }
    return $list;
}
/**
 * 相关产品
 * @param $order 排序 排序 1随机 2最新 3最旧 4价高到底 5价低到高 6销售排行 7人气 
 * @param $type 1全部 2推荐商品
 */
function Related_Product($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $order = $params['order'];
    $jg = $params['jg'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $loop = $params['loop'];
    $type = $params['type'];
    if ($categories_id) {
        $son = spClass("categoriesModel")->spCache(3600 * 24)->Categories_son($categories_id);
        foreach ($son as $k => $v) {
            if ($k == 0) {
                $cidarr = $v['categories_id'];
            } else {
                $cidarr .= ',' . $v['categories_id'];
            }

        }
        $categories_id = ($cidarr <> '') ? $cidarr : $categories_id;
    }
    $selllist = spClass('productsModel')->New_products_in($categories_id, $num, $order,
        $type);
    foreach ($selllist as $k => $v) {
        $list .= $jg[0];

        $list .= '<a class="thumb" href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['products_title'] . '"><img src="' . WEBDIR .
            'upload/' . $v['products_image'] . '" alt="' . $v['detail']['products_title'] .
            '" />' . "\n";
        $list .= '<span class="price">' . Get_price(array(
            'pid' => $v['products_id'],
            'price' => $v['products_price'],
            'yh' => 1,
            'price2' => $v['products_price_discounted'])) . '</span>' . "\n";
        $list .= '<h4>' . $v['detail']['products_name'] . '</h4>' . "\n";
        $list .= '</a>' . "\n" . $jg[1];
    }
    return $list;
}
/**
 * 根据分类ID集合返回这些集合下的商品
 */
function New_products($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $loop = $params['loop'];
    $width = $params['w'];
    $height = $params['h'];
    $width = ($width <> '') ? ' width="' . $width . '"' : '';
    $height = ($height <> '') ? ' height="' . $height . '"' : '';
    if ($categories_id) {
        $son = spClass("categoriesModel")->spCache(3600 * 24)->Categories_son($categories_id);
        foreach ($son as $k => $v) {
            if ($k == 0) {
                $cidarr = $v['categories_id'];
            } else {
                $cidarr .= ',' . $v['categories_id'];
            }

        }
        $categories_id = ($cidarr <> '') ? $cidarr : $categories_id;
    }
    $request = spClass('productsModel')->New_products_in($categories_id, $num, 1);
    $str = '' . sprintf(T('TABLE_HEADING_NEW_PRODUCTS'), strftime('%B')) . '';
    $i = 1;
    $list = '<h2 class="item-h2">' . $str . '</h2>';
    $list .= ' <ul class="product_list">';
    foreach ($request as $k => $v) {
        $row = ($i % $loop == 0) ? '<br class="clear"/>' : '';
        $list .= '<li>';
        $list .= '<a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['products_title'] . '">';
        $list .= '<img src="' . WEBDIR . 'upload/' . $v['products_image'] . '" ' . $width .
            $height . ' alt="' . $v['detail']['products_title'] . '"/>';
        $list .= '<div class="index_pro_des">';
        $list .= Get_price(array(
            'pid' => $v['products_id'],
            'price' => $v['products_price'],
            'yh' => 1,
            'price2' => $v['products_price_discounted']));
        $list .= '<h4>' . $v['detail']['products_name'] . '</h4></div>';
        $list .= '<span class="index_oabg ipd_ed"></span>';
        $list .= '</a></li>' . $row;
        $i = $i + 1;
    }
    $list .= '</ul>';
    return $list;
}
/**
 * 特殊展示方式 根据分类ID集合返回这些集合下的商品
 */
function New_products_div($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $loop = $params['loop'];
    $Sloop = $params['Sloop'];
    $type = $params['type'];
    $width = $params['w'];
    $height = $params['h'];
    $width = ($width <> '') ? ' width="' . $width . '"' : '';
    $height = ($height <> '') ? ' height="' . $height . '"' : '';
    if ($categories_id <> '') {
        $son = spClass("categoriesModel")->spCache(3600 * 24)->Categories_son($categories_id);
        foreach ($son as $k => $v) {
            if ($k == 0) {
                $cidarr = $v['categories_id'];
            } else {
                $cidarr .= ',' . $v['categories_id'];
            }

        }
        $categories_id = ($cidarr <> '') ? $cidarr : $categories_id;
    }
    $request = spClass('productsModel')->New_products_in($categories_id, $num, 1, $type);
    $i = 1;
    $list = '';
    $Sloop = ($Sloop % 2 == 0) ? 'ed' : 'ee';
    foreach ($request as $k => $v) {
        $row = ($i == 1) ? ' class="current "' : '';

        $list .= '<li' . $row . '>' . "\n";
        $list .= '<a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['products_title'] . '">' . "\n";
        $list .= '<img src="' . WEBDIR . 'upload/' . $v['products_image'] . '"' . $width .
            $height . ' alt="' . $v['detail']['products_title'] . '"/>';
        $list .= ' <div class="index_pro_des">' . "\n";
        $list .= '' . Get_price(array(
            'pid' => $v['products_id'],
            'price' => $v['products_price'],
            'price2' => $v['products_price_discounted'])) . '</h5>' . "\n";
        $list .= '<h4>' . $v['detail']['products_name'] . '</h4>';
        $list .= '</div>' . "\n";
        $list .= '<span class="psp index_oabg ipd_' . $Sloop . '"></span>';
        $list .= '</a>' . "\n";
        $list .= '</li>' . "\n";
        //$list .=($i % (int)$loop==0) ? "</div>\n<div>\n":'';
        $i = $i + 1;
    }
    return $list;
}

/**
 * 过滤HTML
 */
function Gl_html($params)
{
    $txt = $params['txt'];
    $type = $params['type'];
    $glstr = ($type == 'True') ? '' : '<img>';
    $glstr .= '<p><br><span><a>';
    return strip_tags($txt, $glstr);
}
/**
 * 最新信息
 * @param $categories_id 分类ID
 * @param $num 显示数量
 * @param $jg 左右符号如array('<li>','</li>')
 * @param $alias 当前别名
 * @param $type 1最新 2推荐 3最热
 */
function Get_News_List($params)
{
    if (!is_array($params))
        return '';
    $c = $params['c'];
    $a = $params['a'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $jg = $params['jg'];
    $loop = $params['loop'];
    $alias = $params['alias'];
    $type = $params['type'];

    $request = spClass('newsModel')->get_news_list($categories_id, $num, $type);

    //dump($request);
    $i = 1;
    foreach ($request as $k => $v) {
        $jg[0] = ($i == $loop) ? str_replace('>', ' class="row">', $jg[0]) : $jg[0];
        $se = ($alias == $v['alias']) ? 'class="selected2"' : '';
        $list .= "\n" . $jg[0] . "\n";
        $list .= '<a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['news_title'] . '">' . $v['detail']['news_name'] .
            '</a>';
        $list .= "\n" . $jg[1] . "\n";
        $i = $i + 1;

    }
    return $list;
}
/**
 * 信息列表2
 */
function Get_News_List2($params)
{
    if (!is_array($params))
        return '';
    $c = $params['c'];
    $a = $params['a'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $jg = $params['jg'];
    $loop = $params['loop'];
    $alias = $params['alias'];
    $type = $params['type'];

    $request = spClass('newsModel')->get_news_list($categories_id, $num, $type);

    //dump($request);
    $i = 1;
    foreach ($request as $k => $v) {
        $jg[0] = ($i == $loop) ? str_replace('>', ' class="row">', $jg[0]) : $jg[0];
        $se = ($alias == $v['alias']) ? 'class="selected2"' : '';
        $list .= "\n" . $jg[0] . "\n";
        $list .= '<a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['news_title'] . '"><img src="' . WEBDIR . 'upload/' .
            $v['news_image'] . '" width="95" height="143" alt="' . $v['detail']['news_title'] .
            '"/></a>' . "\n";
        $list .= '<div class="related_content">' . "\n";
        $list .= '<h4><a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['news_title'] . '">' . $v['detail']['news_name'] .
            '</a></h4>' . "\n";
        $list .= '<p>' . $v['detail']['news_seodescription'] . '</p>' . "\n";
        $list .= '<a class="gray_text fr" href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['news_title'] . '">MORE</a>' . "\n";
        $list .= '</div>' . "\n";
        $list .= "\n" . $jg[1] . "\n";
        $i = $i + 1;

    }
    return $list;
}
/**
 * 特殊排列最新信息
 * @param $categories_id 分类ID
 * @param $num 显示数量
 * @param $jg 左右符号如array('<li>','</li>')
 * @param $loop 多少个换行
 * @param $alias 当前别名
 * @param $type 1最新 2推荐 3最热
 */
function Get_News_List_Div($params)
{
    if (!is_array($params))
        return '';
    $c = $params['c'];
    $a = $params['a'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $jg = $params['jg'];
    $loop = $params['loop'];
    $alias = $params['alias'];
    $type = $params['type'];
    //dump($params);exit;
    $request = spClass('newsModel')->get_news_list($categories_id, $num, $type);

    //dump($request);exit;
    $i = 1;
    foreach ($request as $k => $v) {
        $se = ($alias == $v['alias']) ? 'class="selected2"' : '';
        $list .= "\n" . $jg[0] . "\n";
        $list .= '<div class="news_img_l">';
        $list .= '<a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['news_title'] . '"><img src="' . WEBDIR . 'upload/' .
            $v['news_image'] . '" width="90" height="135" alt="' . $v['detail']['news_title'] .
            '"/></a>';
        $list .= "\n</div>\n";
        $list .= '<div class="news_con_r">' . "\n";
        $list .= '<h4 class="h4title"><a href="' . spUrl($c, $a, array('alias' => $v['alias'])) .
            '" title="' . $v['detail']['news_title'] . '">' . $v['detail']['news_name'] .
            '</a></h4>';
        $list .= '<p class="by_time">' . T('Posted on') . ' ' . format_date_ago(array('date' =>
                $v['news_date_added'])) . ' ' . T('by') . ' <a href="' . spUrl('main', 'index') .
            '">' . STORENAME . '</a><p>';
        $list .= '<p>' . $v['detail']['news_seodescription'] . '</p>' . "\n";
        $list .= "\n</div>\n";
        $list .= "\n" . $jg[1] . "\n";
        //$list .=($i % (int)$loop==0) ? "</div><div>":'';
        $i = $i + 1;
    }
    return $list;
}
/**
 * 关于我们
 * categories_id 分类ID
 * num 显示数量
 * jg 左右符号如array('<li>','</li>')
 * alias 当前别名
 */
function Get_About_list($params)
{
    if (!is_array($params))
        return '';
    $c = $params['c'];
    $a = $params['a'];
    $categories_id = $params['cid'];
    $num = $params['num'];
    $jg = $params['jg'];
    $loop = $params['loop'];
    $alias = $params['alias'];
    $request = spClass('newsModel')->get_news_list($categories_id, $num);
    //dump($request);
    $i = 1;
    foreach ($request as $k => $v) {
        $url = ($v['detail']['news_url']) ? $v['detail']['news_url'] : spUrl($c, $a,
            array('alias' => $v['alias']));
        $jg[0] = ($i == $loop) ? str_replace('>', ' class="row">', $jg[0]) : $jg[0];
        $list .= $jg[0] . '<a href="' . $url . '" title="' . $v['detail']['news_title'] .
            '" rel="nofollow">' . $v['detail']['news_name'] . '</a>' . $jg[1];
        $i = $i + 1;
    }
    return $list;
}
/**
 * 返回信息上一条信息
 */
function Get_Previous($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $cid = $params['cid'];
    $newsid = $params['newsid'];
    $up = spClass('newsModel')->Previous($cid, $newsid);
    $str = '<a href="' . spUrl($c, $a, array('alias' => $up['alias'])) .
        '" rel="prev">' . T('Previous Page') . ':' . $up['detail']['news_name'] . '</a>';
    return $str;
}

/**
 * 返回信息下一条信息
 */
function Get_Next($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $cid = $params['cid'];
    $newsid = $params['newsid'];
    $up = spClass('newsModel')->Next($cid, $newsid);
    $str = '<a href="' . spUrl($c, $a, array('alias' => $up['alias'])) .
        '" rel="next">' . T('Next Page') . ':' . $up['detail']['news_name'] . '</a>';
    return $str;
}
/**
 * 返回信息导航
 */
function Get_BreadCrumb($params)
{
    $categories_id = $params['cid'];
    $jg = $params['jg'];
    $newscategoriesobj = spClass("newscategoriesModel");
    $categoriesidlist = $newscategoriesobj->breadcrumbnews($categories_id); //返回当前父类
    $str = '<a class="location-home" href="' . spUrl('main', 'index') . '">' . T('Navhome') .
        '</a>' . $jg;
    $count = count($categoriesidlist) - 1;
    foreach ($categoriesidlist as $k => $v) {
        $jgstr = $jg;
        $alias = ($k == 0) ? '' : $v['alias'];
        $str2 .= '<span><a href="' . spUrl('news', 'index', array('alias' => $alias)) .
            '">' . $v['categories_name'] . '</a></span>' . $jgstr;
        //$str2.=($k==$count)?$v['categories_name']:'<a href="'.spUrl('news','index',array('alias'=>$alias)).'">'.$v['categories_name'].'</a>'.$jgstr;
    }
    return $str . $str2;
}
/**
 * 返回商品导航
 */
function Get_Product_BreadCrumb($params)
{
    $categories_id = $params['cid'];
    $jg = $params['jg'];
    $categoriesobj = spClass("categoriesModel");
    $categoriesidlist = $categoriesobj->breadcrumb($categories_id); //返回当前父类
    $str = '<a class="location-home" href="' . spUrl('main', 'index') . '">' . T('Navhome') .
        '</a>' . $jg . '<a href="' . spUrl('products', 'index') . '">' . T('All Products') .
        '</a>' . $jg;
    $count = count($categoriesidlist) - 1;
    foreach ($categoriesidlist as $k => $v) {
        $jgstr = ($count <= $k) ? '' : $jg;
        $alias = $v['alias'];
        $str2 .= '<span><a href="' . spUrl('products', 'index', array('alias' => $alias)) .
            '">' . $v['categories_name'] . '</a></span>' . $jgstr;
        //$str2.=($k==$count)?$v['categories_name']:'<a href="'.spUrl('news','index',array('alias'=>$alias)).'">'.$v['categories_name'].'</a>'.$jgstr;
    }
    return $str . $str2;
}
/**
 * 返回分页
 */
function Get_page($params)
{
    $c = $params['c'];
    $a = $params['a'];
    $alias = $params['alias'];
    $order = $params['order'];
    $pager = $params['pager'];
    $total = $pager['total_count'];
    $current_page = $pager['current_page'];
    $page_size = $pager['page_size'];
    $page = spClass("page");
    if ($alias <> '')
        $arr['alias'] = $alias;
    if ($order <> 2 and $order <> '')
        $arr['order'] = $order;
    //$arr = ($alias <> '') ? array('alias' => $alias) : '';
    $pageurl = spUrl($c, $a, $arr);
    return $page->pageft($total, $current_page, $page_size, 1, 0, 0, 5, $pageurl);
}
/**
 * 根据信息ID返回评论数
 */
function get_comment_news_count($params)
{
    $news_id = $params['newsid'];
    $condition = array('news_id' => $news_id);
    $count = spClass('news_commentsModel')->spCache(3600 * 24)->findCount($condition);
    return $count;
}
/**
 * 返回最新信息评论
 * @param $num 条数
 */
function get_comment_news_list($params)
{
    if (!is_array($params))
        return '';
    $c = $params['c'];
    $a = $params['a'];
    $num = $params['num'];
    $jg = $params['jg'];
    $request = spClass('news_commentsModel')->comment_news_list($num);
    foreach ($request as $k => $v) {
        $news = spClass('newsModel')->FindformID($v['news_id']);
        $list .= "\n" . $jg[0] . "\n";
        $list .= '<span class="side-comment"><a href="' . spUrl($c, $a, array('alias' =>
                $news['alias'])) . '#comments' . $v['news_comments_id'] . '"> ' . T('Guest') . $v['news_comments_id'] .
            '</a>';
        $list .= T('Say:') . ' <a href="' . spUrl($c, $a, array('alias' => $news['alias'])) .
            '#comments' . $v['news_comments_id'] . '">' . htmlspecialchars($v['review_text']) .
            '</a></span>' . "\n";
        $list .= "\n" . $jg[1] . "\n";
        $i = $i + 1;
    }
    return $list;
}
/**
 * 根据分类ID返回分类ID信息
 * $cid 分类ID $c控制器 $a动作
 * type=1 返回带链接的否则只返回 名称
 */
function get_newscategories($params)
{

    $cid = $params['cid'];
    $c = $params['c'];
    $a = $params['a'];
    $type = $params['type'];
    $newscategoriesobj = spClass("newscategoriesModel");
    $request = $newscategoriesobj->FindformID($cid); //通过ID获取根下的类信息
    if (!$request)
        return false;
    return ($type == 1) ? '<a href="' . spurl($c, $a, array('alias' => $request['alias'])) .
        '" rel="category tag">' . $request['detail']['categories_name'] . '</a>' : $request['detail']['categories_name'];
}

/**
 * 返回标签
 */
function get_news_tag($params)
{
    $newsid = $params['newsid'];
    $c = $params['c'];
    $a = $params['a'];
    $type = $params['type'];
    $newsobj = spClass("newsModel");

    $request = $newsobj->FindformID($newsid);
    $tags = $request['detail']['news_seokeywords'];
    if ($tags == '')
        return false;
    $tmparray = explode(',', $tags);
    if (count($tmparray) > 1) {
        foreach ($tmparray as $k => $v) {
            $tagarr = array('string' => $v, 'replacestr' => '-');
            $tag = strreplacearr($tagarr);
            $str .= ($type == 1) ? '<a href="' . spurl($c, $a, array('alias' => $tag)) .
                '" rel="tag" title="Tag:' . $v . '">' . $v . '</a>' : $v;
        }
    } else {
        $tagarr = array('string' => $tags, 'replacestr' => '-');
        $tag = strreplacearr($tagarr);
        $str = ($type == 1) ? '<a href="' . spurl($c, $a, array('alias' => $tag)) .
            '" rel="tag" title="Tag:' . $tags . '">' . $tags . '</a>' : $tags;
    }
    return $str;
}

/**
 * 返回信息
 */
function get_news($params)
{
    $newsid = $params['newsid'];
    $newsobj = spClass("newsModel");
    $request = $newsobj->FindformID($newsid);
}


/**
 * 折后百分比计算函数，原价$619.00  折后$99.00 Save: 84% off
 * @param $yj  原价
 * @param $zh  优惠价
 * @param $bfb 最后返回百分比
 */
function saveoff($params)
{
    $yj = $params["yj"];
    $zh = $params["zh"];

    $bfb = round((1 - ($zh / $yj)) * 100);
    return $bfb . '%';
}
/**
 * 返回底部代码
 */
function footother($params)
{
    $type = $params['type'];
    $foot1 = stripslashes(FOOTOTHER); //底部脚本-后台
    $foot2 = stripslashes(FOOTOTHER2); //统计代码-后台
    if ($type == 1)
        return $foot1;
    if ($type == 2)
        return $foot2;
}
/////////////////系统内部函数///////////////////////////////////////////
/**
 * 单个字符过滤函数,如豆号，双引号，冒号，空格之类的
 * @param $string   提交过来的字符串
 * @param $replace  过滤后的字符
 * @param 过滤后返回字符串$str
 */
function strreplaceone($find, $replace, $string)
{
    $string = strtolower($string);
    return str_replace($find, $replace, $string);
}
/**
 * 过滤成空格
 */
function Qkg($str)
{
    $s = str_replace('%20', ' ', $str);
    $s = str_replace('-', ' ', $s);
    $s = str_replace('\'', '', $s);
    $s = addslashes($s);
    return $s;
}

/**
 * 多个字符过滤函数,如豆号，双引号，冒号，空格之类的
 * @param $string   提交过来的字符串
 * @param $replace  过滤后的字符
 * @param 过滤后返回字符串$str
 */
function strreplacearr($params)
{
    // Smarty注册函数的参数只能是一个数组，数组分别指代在模板中的参数，请注意$params的几个值，下面还会说明
    $string = $params["string"];
    $replacestr = $params["replacestr"];
    $glstr = array(
        '\'',
        ' ',
        '"',
        ':');
    //echo $replacestr;
    foreach ($glstr as $v) {
        //echo $v;
        $string = str_replace($v, $replacestr, $string);
        //echo str_replace($v,"John","Hello 'world!");
    }
    return $string;
}
/**
 * 反转义 用stripslashes进行addslashes的反转义 
 */
function stripslashes_value($value)
{
    if (get_magic_quotes_gpc()) {
        return stripslashes($value);
    } else {
        $value = str_replace('\"', '"', $value);
        $value = str_replace("\'", "'", $value);
        return $value;
    }
}
/**
 * 转义 检查magic_quotes_gpc是否打开,如果没打开，用addslashes进行转义
 */
function addslashes_value($value)
{
    if (get_magic_quotes_gpc()) { //系统是否打开转义
        return $value;
    } else {
        return addslashes($value);
    }
}
/**
 * 删除转义字符
 */
function stripslashes_if_gpc_magic_quotes($string)
{
    if (get_magic_quotes_gpc()) {
        return stripslashes($string);
    } else {
        $string = str_replace('\"', '"', $string);
        $string = str_replace("\'", "'", $string);
        return $string;
    }
}
/**
 * 增加转义字符
 */
function addslashes_if_gpc_magic_quotes($string)
{
    if (!get_magic_quotes_gpc()) {
        return addslashes($string);
    } else {
        $string = str_replace('"', '\"', $string);
        $string = str_replace("'", "\'", $string);
        return $string;
    }
}
/**
 * 返回星期几
 * @param $day
 */
function getday($params)
{
    $day = $params["day"];
    $arr = array(
        "天",
        "一",
        "二",
        "三",
        "四",
        "五",
        "六");
    return "星期" . $arr[date("w", strtotime($day))];
}
/**
 * 返回小时分钟前
 *@param $date unix时间
 */
function format_date_ago($params)
{
    $date = $params['date'];
    $limit = time() - $date;
    if ($limit < 60) {
        return $limit . ' second before';
    }
    if ($limit >= 60 && $limit < 3600) {
        return floor($limit / 60) . ' minutes ago';
    }
    if ($limit >= 3600 && $limit < 86400) {
        return floor($limit / 3600) . ' hours ago';
    }
    if ($limit >= 86400) {
        return date('l dS \of F,Y', $date);
    }
}
/**
 * 处理$_SERVER['REQUEST_URI']
 * 和获取$_SESSION['HTTP_REFERER']=$_SERVER['HTTP_REFERER']
 */
function request_uri()
{
    if (isset($_SERVER['REQUEST_URI'])) {
        $uri = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    } else {
        if (isset($_SERVER['argv'])) {
            $uri = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['argv'][0];
        } else {
            $uri = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
        }
    }
    return $uri;
}

/**
 * 获取当前浏览者的IP地址
 */
function getIP()
{
    if (@$_SERVER["HTTP_X_FORWARDED_FOR"])
        $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
    else
        if (@$_SERVER["HTTP_CLIENT_IP"])
            $ip = $_SERVER["HTTP_CLIENT_IP"];
        else
            if (@$_SERVER["REMOTE_ADDR"])
                $ip = $_SERVER["REMOTE_ADDR"];
            else
                if (@getenv("HTTP_X_FORWARDED_FOR"))
                    $ip = getenv("HTTP_X_FORWARDED_FOR");
                else
                    if (@getenv("HTTP_CLIENT_IP"))
                        $ip = getenv("HTTP_CLIENT_IP");
                    else
                        if (@getenv("REMOTE_ADDR"))
                            $ip = getenv("REMOTE_ADDR");
                        else
                            $ip = "Unknown";
    return $ip;
}
/**
 * 作用：取得随机字符串
 * 参数：
 * 1、(int)$length = 32 #随机字符长度，默认为32
 * 2、(int)$mode = 0 #随机字符类型，0为大小写英文和数字，1为数字，2为小写子木，3为大写字母，4为大小写字母，5为大写字母和数字，6为小写字母和数字
 * 返回：取得的字符串
 * 使用：
 * $str = $this->generate_rand($length, $mode);
 */
function generate_rand($length = 32, $mode = 0)
{
    switch ($mode) {
        case '1':
            $str = '1234567890';
            break;
        case '2':
            $str = 'abcdefghijklmnopqrstuvwxyz';
            break;
        case '3':
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
            break;
        case '4':
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
            break;
        case '5':
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
            break;
        case '6':
            $str = 'abcdefghijklmnopqrstuvwxyz1234567890';
            break;
        default:
            $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
            break;
    }

    $result = '';
    $l = strlen($str);

    for ($i = 0; $i < $length; $i++) {
        $num = rand(0, $l);
        $result .= $str[$num];
    }
    return $result;
}
/**
 * 生成随机字符串
 */
function random($length)
{
    $hash = '';
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max = strlen($chars) - 1;
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    for ($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}
/**
 * 登入用户生产验证字符串
 */
function auth_key($str)
{
    $str=($str<>'')?$str:$_COOKIE['_FSESSID'];
    $timestamp = date('y-m-d');
    $auth = substr(md5(AUTH_KEY . getIP() . $str . $timestamp), 8, 6);
    return $auth;
}
/**
 * 
 *用户信息加密解密函数  
 *待加密内容用\t分割  
 *@return String 加密或解密字符串  
 *@param String $string 待加密或解密字符串  
 *@param String $operation 操作类型定义 DECODE=解密 ENDODE=加密  
 *@param String $key 加密算子  
 */
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
{
    $ckey_length = 4;
    $key = md5($key ? $key : UC_KEY);
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) :
        substr(md5(microtime()), -$ckey_length)) : '';

    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);

    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) :
        sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb),
        0, 16) . $string;
    $string_length = strlen($string);

    $result = '';
    $box = range(0, 255);

    $rndkey = array();
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }

    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }

    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }

    if ($operation == 'DECODE') {
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) &&
            substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc . str_replace('=', '', base64_encode($result));
    }

}
/**
 * 后台返回用户名
 */
function getname()
{
    $user = spClass("userModel")->userinfo();
    return $user['firstname'] . $user['lastname']; // 简单起见，直接通过PHP的date函数来处理，当然开发者可以进行更复杂的操作
}
/**
 * 操作记录存储
 * 
 */
function Savelog($loginfo)
{
    $u = spClass("userModel")->userinfo();
    $uname = $u['uname'];
    $loginfo = $uname . " " . $loginfo . "\r\n";
    //记录日志
    $handle = fopen("log/" . Date('Ymd') . ".log", 'a+');
    fwrite($handle, Date('Y-m-d H:i:s') . ' ' . $loginfo);
    fclose($handle);
}
/**
 * 返回404错误页面
 * @type 1 是新闻 2是商品
 */
function ERROR404($type)
{
    eval($GLOBALS['G_SP']["dispatcher_error"]);
}
